package com.example.casper.jnuexperiment2025;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

public class ImageViewActivity extends AppCompatActivity {
    private Button buttonPrevious, buttonNext;
    private ImageView imageViewFunny;
    private int[] imageIDArray = {
            R.drawable.funny_1, R.drawable.funny_2,
            R.drawable.funny_3, R.drawable.funny_4,
            R.drawable.funny_5, R.drawable.funny_6
    };
    private int imageIDArrayCurrentIndex = 0;

    public ImageViewActivity() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_view);

        // 在这里初始化视图！
        buttonPrevious = findViewById(R.id.button_previous);
        buttonNext = findViewById(R.id.button_next);
        imageViewFunny = findViewById(R.id.image_view_funny);

        imageViewFunny.setImageResource(imageIDArray[imageIDArrayCurrentIndex]);

        buttonPrevious.setOnClickListener(new MyButtonClickListener());
        buttonNext.setOnClickListener(new MyButtonClickListener());
    }

    private class MyButtonClickListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            if (view.getId() == R.id.button_next) {
                imageIDArrayCurrentIndex++;
                if (imageIDArrayCurrentIndex >= imageIDArray.length) {
                    imageIDArrayCurrentIndex = 0;
                }
            } else if (view.getId() == R.id.button_previous) {
                imageIDArrayCurrentIndex--;
                if (imageIDArrayCurrentIndex < 0) {
                    imageIDArrayCurrentIndex = imageIDArray.length - 1;
                }
            }
            imageViewFunny.setImageResource(imageIDArray[imageIDArrayCurrentIndex]);
        }
    }
}